<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cole System Manage</title>
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <script src="/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="" id="residentForm">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">住户姓名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="name" autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">性别</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="gender" autocomplete="off" class="layui-input">
                                </div>
                            </div>



                            <div class="layui-inline">
                                <label class="layui-form-label">电话</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="tel" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-warm data-delete-btn" lay-event="templatedownload"> 模板下载 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-normal data-delete-btn" lay-event="export"> 导出 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-warm data-delete-btn" lay-event="import">导入 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-normal data-delete-btn" lay-event="bar"> 楼号与居民柱状图 </button>
                    
                </div>


                
            </script>




            <table class="layui-hide" id="residentTable" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            </script>

        </div>
    </div>


    <script id="picTpl">
        <img src="{{d.picture}}" name="picture" style="width:35px; height:100%;" />
    </script>

    <script>
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;

            table.render({
                elem: '#residentTable',
                url: 'http://localhost:8080/resident/residentList',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    { type: "checkbox", width: 50 },
                    { field: 'id', width: 80, title: 'ID', sort: true },
                    { field: 'name', width: 80, title: '住户姓名' },
                    { field: 'gender', width: 80, title: '性别', sort: true },
                    { field: 'tel', title: '联系方式', minWidth: 180 },
                    { field: 'idcard', width: 200, title: '身份证', sort: true },
                    { field: 'building', width: 80, title: '楼号', sort: true },
                    { field: 'unit', width: 80, title: '单元', sort: true },
                    { field: 'room', width: 80, title: '房号', sort: true },
                    { field: 'picture', width: 150, title: '住户照片', templet: '#picTpl' },

                    { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
                ]],
                limits: [5, 10, 15, 20, 25, 50, 100],
                limit: 5,
                page: true,
                skin: 'line'
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = JSON.stringify(data.field);
                console.log(result);
                layer.alert(result, {
                    title: '搜索信息'
                });

                //执行搜索重载
                table.reload('residentTable', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        // searchParams: result
                        //发送给后台的数据
                        name: data.field.name,
                        gender: data.field.gender,
                        tel: data.field.tel,
                    }
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加员工信息',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['80%', '80%'],
                        content: 'residentAdd.html',
                        end: function (index) {
                            // 列表重新加载
                            table.reload('residentTable');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus('residentTable')
                        , data = checkStatus.data;

                    var dataJsonStr = JSON.stringify(data);



                    // layer.alert(JSON.stringify(data));
                    layer.alert("确定删除选中员工？");
                    console.log(data)

                    $.ajax({
                        url: "http://localhost:8080/employee/deleteMany",
                        type: "get",
                        data: dataJsonStr,
                        success: function (result) {
                            if (result.code == 20000) {

                            } else {
                                alert("删除失败")
                            }
                        },
                        dataType: "json"
                    })
                    //柱状图
                } else if (obj.event === 'bar') {
                    layer.open({
                        title: '',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['51%', '65%'],
                        content: 'bar.html',
                        end: function (index) {
                            // 列表重新加载
                            table.reload('residentTable');
                        }
                    })
                } else if (obj.event === 'export') {

                    location.href = "http://localhost:8080/resident/export?" + $("#residentForm").serialize();  // 序列化
                } else if (obj.event === 'import') {
                    layer.open({
                        title: '学员信息导入',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['60%', '80%'],
                        content: 'residentImport.html',
                        end: function (index) {
                            table.reload('residentTable');
                        }
                    })
                } else if (obj.event === 'templatedownload') {
                    //模板下载
                    location.href = "http://localhost:8080/resident/templateDownload"
                }
            });


            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {

                    //存数据
                    localStorage.setItem("resident", JSON.stringify(data));

                    var index = layer.open({
                        title: '编辑用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['80%', '100%'],
                        content: '/page/building/residentEdit.html',
                        end: function (index) {
                            // 列表重新加载
                            table.reload('residentTable');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "http://localhost:8080/resident/delete",
                            type: "get",
                            data: data,
                            success: function (result) {
                                if (result.code == 0) {
                                    obj.del();
                                    table.reload('residentTable');
                                } else {
                                    alert("删除失败")
                                }
                            },
                            dataType: "json"
                        })

                        layer.close(index);
                    });
                }
            });

        });
    </script>
</body>

</html>